package com.ytzl.itrip.service.mail.impl;

import com.ytzl.itrip.service.mail.MailService;
import com.ytzl.itrip.util.DataCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * Description: 本类用于发送邮件
 * Created by gengshu on 2019/12/24 15:08
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Value("${spring.mail.username}")  // 获取yml中的配置
    private String MAIL_SENDER; //邮件发送者

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendSimpleMail(String mailTo, String activationCode) {
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            //邮件发送人
            simpleMailMessage.setFrom(MAIL_SENDER);
            //邮件接收人
            simpleMailMessage.setTo(mailTo);
            //邮件主题
            simpleMailMessage.setSubject("爱旅行【邮件】");
            //邮件内容
            simpleMailMessage.setText(activationCode);
            DataCacheUtil.map.put(mailTo,activationCode);
            javaMailSender.send(simpleMailMessage);
        } catch (Exception e) {
            log.error("邮件发送失败", e.getMessage());
        }
    }

}
